package com.xiang.plane.view;

/**
 * Created by xiangliyou on 2017/8/9.
 */


import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class TestGlSurfaceView extends GLSurfaceView {
    MyGLRender myRender;
    float vertexArray[] = new float[]{-0.8f, -0.4f * 1.732f, 0.0f, 0.8f, -0.4f * 1.732f, 0.0f, 0.0f, 0.4f * 1.732f, 0.0f};
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertexArray.length * 4);
    FloatBuffer fb;

    public TestGlSurfaceView(Context context) {
        super(context);
        vbb.order(ByteOrder.nativeOrder());
        fb = vbb.asFloatBuffer();
        fb.put(vertexArray);
        fb.position(0);
        myRender = new MyGLRender();

        setRenderer(myRender);
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                myRender.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1);
            }
        });
        return super.onTouchEvent(event);
    }

    private class MyGLRender implements Renderer {

        @Override
        public void onDrawFrame(GL10 gl) {
            gl.glClearColor(mRed, mGreen, mBlue, 0.8f);//设置屏幕颜色
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);// 清空缓存

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            gl.glViewport(0, 0, width, height);//设置窗口
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        }

        public void setColor(float r, float g, float b) {
            mRed = r % 10;
            mGreen = g % 10;
            mBlue = b % 10;
            System.out.println("R " + mRed + "   G" + mGreen + "    B" + mBlue);
        }

        private float mRed;
        private float mGreen;
        private float mBlue;
    }
}

